home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / codecs / drawtextcodec / drawtextcompress.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.0 KB  |  233 lines

  1. /*
  2.     File:        DrawTextCompress.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Mark Krueger    
  7.  
  8.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/29/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include    <Types.h>
  25. #include    <Memory.h>
  26. #include    <ImageCompression.h>
  27. #include    <QuickDraw.h>
  28.  
  29.  
  30.  
  31. #include    "DrawTextCodec.h"
  32.  
  33. short
  34. CalcError(register char *src,short rowBytes,short index,register char *table);
  35. long
  36. Compress(char *baseAddr,short rowBytes,char *pBaseAddr,short pRowBytes,
  37.     short bwidth,short bheight,long spatialQ,long temporalQ,char *dataPtr,
  38.     Fixed *similarityP,SharedGlobals *sGlob);
  39. void InitCharTab(SharedGlobals *sGlob,ComponentInstance self);
  40. void MakeSkipTable(SharedGlobals *sGlob);
  41.  
  42. /***********************
  43.     
  44.     
  45.     Compress bheight strips ( of bwidth blocks ) of pixels using the given table
  46.     of char values.
  47.     
  48.     Source must be 1-bit/pixel.
  49.     
  50.     Should be called in 32-bit mmu mode, since we access pixels directly.
  51.     
  52.  
  53.  
  54. ***********************/
  55.  
  56. long
  57. Compress(char *baseAddr,short rowBytes,char *pBaseAddr,short pRowBytes,
  58.     short bwidth,short bheight,long spatialQ,long temporalQ,char *dataPtr,
  59.     Fixed *similarityP,SharedGlobals *sGlob)
  60. {
  61.     #pragma unused(pBaseAddr,pRowBytes,temporalQ)
  62.     char *dataStart = dataPtr;
  63.     char *bp;
  64.     register short            i;
  65.     short            x,y;
  66.     short            min = 1000,minIndex,e;
  67.     char            *charTab = sGlob->table;
  68.     short            startIndex = ' ',endIndex = 'z';
  69.     
  70.     
  71.     /* 
  72.     
  73.         limit search based on requested quality level.
  74.         
  75.     */
  76.     
  77.     if ( spatialQ <= codecLowQuality ) {
  78.         startIndex = ' ';
  79.         endIndex = '9';
  80.     }
  81.     else if ( spatialQ <= codecNormalQuality ) {
  82.         startIndex = ' ';
  83.         endIndex = 'z';
  84.     } else  {
  85.         startIndex = 0;
  86.         endIndex = 255;
  87.     }
  88.     
  89.     /* 
  90.     
  91.         compress the strips
  92.         
  93.     */
  94.     
  95.     for ( y=0; y <  bheight; y++ ) {
  96.         bp = baseAddr;
  97.         for (x=0; x < bwidth; x++) {
  98.             min = 1000;
  99.             
  100.             /* find the best character for this block */
  101.             
  102.             for (i=startIndex; i <= endIndex; i++) {
  103.                 if ( (e=CalcError(bp,rowBytes,i,charTab)) < min ) {
  104.                     min = e;
  105.                     minIndex = i;
  106.                 }
  107.                 if ( min == 0 )
  108.                     break;
  109.             }
  110.             *dataPtr++ = minIndex;                    // write char as compressed data
  111.             bp++;
  112.         }
  113.         baseAddr += rowBytes * FONT_HEIGHT;            // bump to next strip
  114.     }
  115.     if ( similarityP )                                // if we did similarity...
  116.         *similarityP = 0;
  117.     return(dataPtr - dataStart);
  118. }
  119.  
  120.  
  121. /************************
  122.  
  123.     Calculate and return the number of pixels that dont match between the given block of 
  124.     1-bit pixels and the equivcalent indexed block in the table
  125.     
  126. ************************/
  127.  
  128. short
  129. CalcError(register char *src,short rowBytes,short index,register char *table)
  130. {
  131.     short err = 0;
  132.     short i,j;
  133.     register signed char d;
  134.     
  135.     table += index;
  136.     
  137.     for ( i=FONT_HEIGHT; i-- ; ) {
  138.         d = *src ^ *table;
  139.         if ( d != 0 ) {
  140.             if ( d == 0xff )
  141.                 err += 8;
  142.             else {
  143.                 for ( j =FONT_WIDTH; j--;  )  {            
  144.                     if ( d < 0 ) 
  145.                         err++;
  146.                     d <<= 1;
  147.                 }
  148.             }
  149.         }
  150.         table += (256 * FONT_WIDTH) / 8;
  151.         src += rowBytes;
  152.     }
  153.     return(err);
  154. }
  155.  
  156.  
  157. /************************
  158.  
  159.     Build a bitmap table of the characters in the font. Keep as shared data.
  160.     
  161. ************************/
  162.  
  163. void InitCharTab(SharedGlobals *sGlob,ComponentInstance self)
  164.  
  165. {
  166.     CGrafPtr         savePort;
  167.     GDHandle         saveGD;
  168.     short            x;
  169.     Rect            rect;
  170.     GWorldPtr        gw;
  171.     THz                saveZone;
  172.     Boolean            inAppHeap;
  173.         
  174.     /*
  175.     
  176.         figure out which zone to use, based on where we are loaded.
  177.         
  178.     */
  179.     
  180.     saveZone = GetZone();
  181.     inAppHeap = ( GetComponentInstanceA5(self) != 0 );
  182.     if ( !inAppHeap )
  183.         SetZone(SystemZone());
  184.     sGlob->tableWorld = nil;
  185.     sGlob->table = nil;
  186.     GetGWorld(&savePort,&saveGD);
  187.     SetRect(&rect,0,0,256*FONT_WIDTH,FONT_HEIGHT);
  188.     if ( NewGWorld(&gw,1,&rect,nil,nil,0) == 0 ) {
  189.         (*gw->portPixMap)->rowBytes = 0x8000 | (256 * FONT_WIDTH) / 8;
  190.         SetGWorld(gw,nil);
  191.         TextSize(FONT_SIZE);
  192.         TextFont(FONT_ID);
  193.         EraseRect(&rect);
  194.         ClipRect(&rect);
  195.         for (x=0; x < 256; x++) {
  196.             MoveTo(x*FONT_WIDTH,BASE_LINE);
  197.             DrawChar(x);
  198.         }
  199.         SetGWorld(savePort,saveGD);
  200.         LockPixels(gw->portPixMap);
  201.         sGlob->tableWorld = gw;
  202.         sGlob->table = GetPixBaseAddr(gw->portPixMap);
  203.         
  204.     } 
  205.     SetZone(saveZone);
  206. }
  207.  
  208.  
  209. /************************
  210.  
  211.     Figure duplicate font chars. not used.
  212.     
  213. ************************/
  214.  
  215. void MakeSkipTable(SharedGlobals *sGlob)
  216.  
  217. {
  218.     short        i,x;
  219.     char        *charTab = sGlob->table;
  220.     
  221.     for (i=0; i < 256; i++) 
  222.         sGlob->skipTable[i] = 0;
  223.     for (i=0; i < 256; i++) {
  224.         char *icp = charTab + i * FONT_WIDTH;
  225.         if ( !sGlob->skipTable[i] ) {
  226.             for (x=i+1; x < 256; x++) {
  227.                 if ( CalcError(icp,(256 * FONT_WIDTH) / 8,x,charTab) == 0) 
  228.                     sGlob->skipTable[x] = 1;
  229.             }
  230.         }
  231.     }
  232.  
  233. }